#include "Questions.h"

Questions::Questions(Arena* arena, const char* filename, int correct_answer, int id)
{
	m_correct_answer = correct_answer;
	m_arena = arena;

	m_xpos = 0;
	m_ypos = 0;
	m_is_answered = false;

	m_bitmap = al_load_bitmap("assets/question_mark.png");
	al_convert_mask_to_alpha(m_bitmap, al_map_rgb(255,0,255));

	m_question = al_load_bitmap(filename);
	m_question_width = al_get_bitmap_width(m_question);
	m_question_height = al_get_bitmap_height(m_question);
	m_question_width /= 3;

	m_width = al_get_bitmap_width(m_bitmap);
	m_width /= 4;
	m_height = al_get_bitmap_height(m_bitmap);

	m_current_frame = 0;
	m_current_answer = 0;
		
	int random_number = rand() % 10;
	if(random_number < 5)
		m_is_device = false;
	else
		m_is_device = true;

	m_id = id;
}

bool Questions::Answered()
{
	if(m_current_answer == m_correct_answer)
	{
		m_is_answered = true;
		m_xpos = 0;
		m_ypos = 0;
		m_current_frame = 0;
		return true;
	}
	else
	{
		return false;
	}
}

int Questions::getID()
{
	return m_id;
}

void Questions::Spawn()
{
	//random x and y position on grid, which is not a bit of terrain
	int randX, randY;
	do
	{
		randX = rand() % 40;
		randY = rand() % 30;
	}while(m_arena->IsEmpty(randX*20, randY*20) != '.');

	m_xpos = randX*20;
	m_ypos = randY*20;
}

void Questions::Render()
{
	if(!m_is_answered)
		al_draw_bitmap_region(m_bitmap, m_current_frame*m_width, 0, m_width, m_height, m_xpos, m_ypos, 0);
}

bool Questions::IsDevice()
{
	return m_is_device;
}

void Questions::NextFrame()
{
	if(m_current_frame < 3)
		m_current_frame++;
	else
		m_current_frame = 0;
}

bool Questions::CollideWithPlayer(player* guy)
{
	if( guy->getX() == m_xpos && guy->getY() == m_ypos)
		return true;
	else
		return false;
}

void Questions::RenderQuestion()
{
	if(!m_is_answered)
		al_draw_bitmap_region(m_question, m_current_answer*m_question_width, 0, m_question_width, m_question_height, 200, 150, 0);
}

void Questions::NextAnswer()
{
	if(m_current_answer < 2)
		m_current_answer++;
	else
		m_current_answer = 0;
}

void Questions::PreviousAnswer()
{
	if(m_current_answer < 1)
		m_current_answer = 2;
	else
		m_current_answer--;
}